package com.tanhua.controller;

import com.tanhua.db.UserInfo;
import com.tanhua.service.UserService;
import com.tanhua.db.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

/**
 * @Author: cfl
 * @CreateTime: 2021-06-18 20:15
 * @Description: 业务控制 接收参数 返回响应
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    // 根据用户手机号查询用户
    @GetMapping(value = "/findByPhone", name = "根据用户手机号查询用户")
    public ResponseEntity findByPhone(String phone){
        return userService.findByPhone(phone);
    }

    // 保存用户
    @PostMapping(value = "/saveUser", name = "保存用户")
    public ResponseEntity saveUser(@RequestBody User user){ // 从请求体中拿数据
        return userService.saveUser(user);
    }

    // 用户登陆注册发送短信
    @PostMapping(value = "/login", name = "发送短信验证码")
    public ResponseEntity login(@RequestBody Map<String,String> paramMap){
        String phone = paramMap.get("phone");
        return userService.login(phone);
    }

/*    @PostMapping(value = "/login", name = "发送短信验证码")
    public ResponseEntity login(@RequestBody String paramMap){
        System.out.println("paramMap = " + paramMap);
        return userService.login(paramMap);
    }*/

    // 用户登陆验证判断
    @PostMapping(value = "/loginVerification", name = "用户登陆/注册验证判断")
    public ResponseEntity loginVerification(@RequestBody Map<String,String> paramMap){
        // 获取手机号 和 号验证码
        String phone = (String) paramMap.get("phone");
        String vCode = (String) paramMap.get("verificationCode");
        System.out.println("vCode = " + vCode);
        return userService.loginVerification(phone,vCode);
    }

    // 完善用户基础信息
    @PostMapping(value = "/loginReginfo",name="完善用户基本信息")
    public ResponseEntity loginReginfo(@RequestBody UserInfo userInfo,
                                       @RequestHeader("Authorization") String token){
        return userService.loginReginfo(userInfo,token);
    }

    // 完善用户头像信息 并验证头像识别
    @PostMapping(value = "/loginReginfo/head",name="完善用户头像信息")
    public ResponseEntity headPicInfo(MultipartFile headPhoto,  // org.springframework.web.multipart.MultipartFile;
                                @RequestHeader("Authorization") String token) throws IOException {
        return userService.headPicInfo(headPhoto,token);
    }

}
